package com.guxingke.gblog.action;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.interceptor.ServletRequestAware;

import com.guxingke.gblog.dto.AboutDto;
import com.guxingke.gblog.dto.ArticleDto;
import com.guxingke.gblog.dto.ArticleLiteDto;
import com.guxingke.gblog.dto.MsgDto;
import com.guxingke.gblog.service.ArticleService;
import com.guxingke.gblog.service.MsgService;
import com.guxingke.gblog.service.impl.ArticleServiceImpl;
import com.guxingke.gblog.service.impl.MsgServiceImpl;
import com.opensymphony.xwork2.ActionSupport;

public class ArticleAction extends ActionSupport implements ServletRequestAware {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private ArticleService articleService = new ArticleServiceImpl();
	private MsgService msgService = new MsgServiceImpl();

	private String articleId;
	private ArticleDto article;
	private AboutDto about;
	private ArticleLiteDto preArticle;
	private ArticleLiteDto nextArticle;
	private List<ArticleLiteDto> archives = null;
	private List<MsgDto> msgs;
	private final int type = 0;
	private final int replytype = 2;
	private String pid;
	private String mainPage;

	@Override
	public String execute() throws Exception {
		System.out.println("execute method");
		return SUCCESS;
	}

	public String archive() throws Exception {
		archives = articleService.getArchive();
		mainPage = "template/archive/detail.jsp";
		return SUCCESS;
	}

	public String about() throws Exception {
		about = articleService.getAbout();
		mainPage = "template/about/detail.jsp";
		return SUCCESS;
	}

	public String detail() throws Exception {
		articleId = request.getParameter("articleId");
		article = articleService.getArticle(Integer.parseInt(articleId));

		/*
		 * article.setClicks(article.getClicks() + 1);
		 * articleService.updateArticle(article);
		 */

		preArticle = articleService.getPreArticle(Integer.parseInt(articleId));
		nextArticle = articleService
				.getNextArticle(Integer.parseInt(articleId));
		msgs = msgService.getComments(article.getId());
		pid = articleId;
		mainPage = "template/article/detail.jsp";
		return SUCCESS;
	}

	public String getArticleId() {
		return articleId;
	}

	public void setArticleId(String articleId) {
		this.articleId = articleId;
	}

	public ArticleDto getArticle() {
		return article;
	}

	public void setArticle(ArticleDto article) {
		this.article = article;
	}

	public AboutDto getAbout() {
		return about;
	}

	public void setAbout(AboutDto about) {
		this.about = about;
	}

	public ArticleLiteDto getPreArticle() {
		return preArticle;
	}

	public void setPreArticle(ArticleLiteDto preArticle) {
		this.preArticle = preArticle;
	}

	public ArticleLiteDto getNextArticle() {
		return nextArticle;
	}

	public void setNextArticle(ArticleLiteDto nextArticle) {
		this.nextArticle = nextArticle;
	}

	public List<ArticleLiteDto> getArchives() {
		return archives;
	}

	public void setArchives(List<ArticleLiteDto> archives) {
		this.archives = archives;
	}

	public List<MsgDto> getMsgs() {
		return msgs;
	}

	public void setMsgs(List<MsgDto> msgs) {
		this.msgs = msgs;
	}

	public String getPid() {
		return pid;
	}

	public void setPid(String pid) {
		this.pid = pid;
	}

	public String getMainPage() {
		return mainPage;
	}

	public void setMainPage(String mainPage) {
		this.mainPage = mainPage;
	}

	public int getType() {
		return type;
	}

	public int getReplytype() {
		return replytype;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

}
